package com.socialcoco.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.socialcoco.entity.*;
import com.socialcoco.serviceImpl.*;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AuthRealm extends AuthorizingRealm {
    @Autowired
    ShiroServiceImpl shiroService;
    @Autowired
    RoleServiceImpl roleService;
    @Autowired
    UserRoleServiceImpl user_roleService;
    @Autowired
    PermissionServiceImpl permissionService;
    @Autowired
    RolePermissionServiceImpl role_permissionService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        User user = (User) principal.getPrimaryPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        Map HashMap = new HashMap<>();
        System.out.println();
        QueryWrapper<UserRole> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id",user.getUid());
        List<UserRole> list = user_roleService.list(userQueryWrapper);
        Map roleMap = new HashMap<>();
        System.out.println("zzh"+list.toString());
        roleMap.put("role_id",list.get(0).getRoleId());
        List<Role> roleList = roleService.listByMap(roleMap);
        for (Role o : roleList) {
            simpleAuthorizationInfo.addRole(o.getRname());
            Map role_permission_map=new HashMap<>();
            role_permission_map.put("role_id",o.getRid());
            List<RolePermission> list1 = role_permissionService.listByMap(role_permission_map);
            for (RolePermission role_permission : list1) {
                Map permission_map=new HashMap<>();
                permission_map.put("permission_id",role_permission.getPids());
                List<Permission> list2 = permissionService.listByMap(permission_map);
                for (Permission p : list2) {
                    simpleAuthorizationInfo.addStringPermission(p.getPermission());
                }
            }
        }
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String accessToken = (String) token.getPrincipal();
        SysToken tokenEntity = shiroService.findByToken(accessToken);
        if (tokenEntity == null || tokenEntity.getExpireTime().isBefore(LocalDateTime.now())) {
            throw new IncorrectCredentialsException("token失效，请重新登录");
        }
        //3. 调用数据库的方法, 从数据库中查询 username 对应的用户记录
        System.out.println("aaaa"+tokenEntity.toString());
        User user = shiroService.findByUserId(tokenEntity.getUserId());
        //4. 若用户不存在, 则可以抛出 UnknownAccountException 异常
        if (user == null) {
            throw new UnknownAccountException("用户不存在!");
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, accessToken, this.getName());
        return info;
    }
}
